package com.example.clustermarkerdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.wearable.activity.WearableActivity;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnMapLoadedListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amp.apis.lib.ClusterClickListener;
import com.amp.apis.lib.ClusterItem;
import com.amp.apis.lib.ClusterOverlay;
import com.amp.apis.lib.ClusterRender;
import com.casio.wsdf30.R;

import java.util.List;

public class MainActivity extends WearableActivity implements ClusterRender,
        OnMapLoadedListener, ClusterClickListener {
    private AMap mAMap;
    private MapView mMapView;
    private TextView infoTextView;
    // Test By Tab ABC
    private int clusterRadius = 80;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity_main);
        // 初始化地图控件
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        init();
    }


    /**
     * marker点击时跳动一下
     */
    public void jumpPoint(final Marker marker) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        Projection proj = mAMap.getProjection();
        final LatLng markerLatlng = marker.getPosition();
        Point markerPoint = proj.toScreenLocation(markerLatlng);
        markerPoint.offset(0, -100);
        final LatLng startLatLng = proj.fromScreenLocation(markerPoint);
        final long duration = 1500;

        final Interpolator interpolator = new BounceInterpolator();
        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);
                double lng = t * markerLatlng.longitude + (1 - t)
                        * startLatLng.longitude;
                double lat = t * markerLatlng.latitude + (1 - t)
                        * startLatLng.latitude;
                marker.setPosition(new LatLng(lat, lng));
                if (t < 1.0) {
                    handler.postDelayed(this, 16);
                }
            }
        });
    }


    private void init() {
        if (mAMap == null) {
            // 初始化地图
            mAMap = mMapView.getMap();
            mAMap.setOnMapLoadedListener(this);
            // 初始化控件和事件
            infoTextView = (TextView) findViewById(R.id.infoview);

        }
    }

    public Drawable getDrawAble(int clusterNum) {
        int radius = dp2px(getApplicationContext(), clusterRadius);
        if (clusterNum == 1) {
            return getApplication().getResources().getDrawable(
                    R.drawable.icon_openmap_mark);
        } else if (clusterNum < 5) {
            BitmapDrawable drawable = new BitmapDrawable(drawCircle(radius,
                    Color.argb(159, 210, 154, 6)));
            return drawable;
        } else if (clusterNum < 10) {
            BitmapDrawable drawable = new BitmapDrawable(drawCircle(radius,
                    Color.argb(199, 217, 114, 0)));
            return drawable;
        } else {
            BitmapDrawable drawable = new BitmapDrawable(drawCircle(radius,
                    Color.argb(235, 215, 66, 2)));
            return drawable;
        }

    }

    @Override
    public void onMapLoaded() {
        // TODO Auto-generated method stub

        ClusterOverlay clusterOverlay = new ClusterOverlay(mAMap,
                dp2px(getApplicationContext(), clusterRadius),
                getApplicationContext());
        clusterOverlay.setClusterRenderer(this);
        clusterOverlay.setOnClusterClickListener(this);
        for (int i = 0; i < ConstantUtils.latlngs.length; i++) {
            RegionItem regionItem = new RegionItem(ConstantUtils.latlngs[i],
                    ConstantUtils.names[i]);
            clusterOverlay.addClusterItem(regionItem);
        }
        for (int i = 0; i < ConstantUtils.latlngs.length; i++) {

            MarkerOptions markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
                    .position(ConstantUtils.latlngs[i])
                    .draggable(true);
            mAMap.addMarker(markerOption);
        }
    }

    @Override
    public void onClick(Marker marker, List<ClusterItem> clusterItems) {
        String content = "";
        for (ClusterItem clusterItem : clusterItems) {
            RegionItem item = (RegionItem) clusterItem;
            content += item.getTitle() + " ";
        }
        infoTextView.setText(content);
        if (1 == clusterItems.size()) {
            Toast.makeText(getApplication(), content, Toast.LENGTH_LONG).show();
            jumpPoint(marker);
        }

    }

    private Bitmap drawCircle(int radius, int color) {
        Bitmap bitmap = Bitmap.createBitmap(radius * 2, radius * 2,
                Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        RectF rectF = new RectF(0, 0, radius * 2, radius * 2);
        paint.setColor(color);
        canvas.drawArc(rectF, 0, 360, true, paint);
        return bitmap;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    // -----------------------生命周期必须重写的方法-----------------------------

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

}
